package com.example.onlinemusicserver2024.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.onlinemusicserver2024.common.JwtUtils;
import com.example.onlinemusicserver2024.common.R;
import com.example.onlinemusicserver2024.model.domain.Consumer;
import com.example.onlinemusicserver2024.model.domain.Token;
import com.example.onlinemusicserver2024.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 用户表consumer的控制层
 */
@RestController
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    /**
     * 用于请求所有的用户数据
     * 添加分页的参数
     */
    @GetMapping("/user")
    public R allUser(@RequestParam(defaultValue = "1") int curPage, @RequestParam(defaultValue = "1") int pageSize) {
        System.out.println("查询所有用户数据。。。");
        IPage<Consumer> page = new Page<>();
        // 设置当前页
        page.setCurrent(curPage);
        // 设置每页大小
        page.setSize(pageSize);
        // todo 查询所有的用户数据，并返回给前端
        R result = consumerService.allUser(page);
        return result;
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    // http://localhost:8888/user/delete?id=5
    @GetMapping("/user/delete")
    public R deleteUser(@RequestParam int id) {
        System.out.println("删除用户，id=" + id);
        R result = consumerService.deleteUser(id);
        return result;
    }


    // 用于批量删除用户
    @DeleteMapping("/user/deleteIds")
    public R deleteUsers(@RequestParam String[] ids) {
        System.out.println("批量删除。。");
        R result = consumerService.deleteByIds(ids);
        return result;
    }

    @GetMapping("/user/detail")
    public R userDetail(@RequestParam int id) {
        R result = consumerService.userDetail(id);
        return result;
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/users")
    public R getAllUsers() {
        R result = consumerService.getAllUsers();
        return result;
    }

    /**
     * 注册
     * @param consumer
     * @return
     */
    @PostMapping("/user/add")
    public R addUser(@RequestBody Consumer consumer) {
        System.out.println("注册用户：" + consumer.toString());
        R result = consumerService.addUser(consumer);
        return result;
    }

    /**
     * 客户端的登录
     * @param consumer
     * @return
     */
    @PostMapping("/user/login/status")
    public R loginStatus(@RequestBody Consumer consumer, HttpSession session){
        R result = consumerService.loginStatus(consumer,session);
        // 登录成功后再添加Token的数据，返回给客户端
        if(result.getSuccess()){
            String token = JwtUtils.generateToken(consumer.getUsername());
            Token t = new Token();
            t.setAuthorization(token);
            t.setObj(result.getData());
            result.setData(t);
        }
        return result;
    }

}
